<?xml version="1.0" encoding="utf-8" ?>
[#include "/WEB-INF/pages/common/taglibs.ftl"]
[#macro _script]
	<style type="text/css">
			.configuration {
				padding:0;
				margain:0;
			}
			.config_edit {
				margin-left:762px;
			}
			.config_list {
				float:left;
				width:750px;
				margin-left:8px;
			}
	</style>
		
	<script type="text/javascript">
		var base = (function(){
			// 事件绑定
			function _addEventListeners() {
				// 维护
				$('a[act=edit]').unbind('click').click(function(){
					var	id = $(this).parent().attr('tval');
					[@c.url	var="editUrl" value='/manage/Config_edit.action' scope="page"]
						[@c.param name="backurl" value="${currentUrl}"/]
					[/@c.url]
					var	url	= "${editUrl}";
					$.get(url, {'id':id, 'ajax':'true'}, function(data) {
						$('.config_edit').html(data);
						base.init();
					})
					//window.location	= url;
				})
				
				// 删除
				$('a[act=delete]').unbind('click').click(function(){
					var	id = $(this).parent().attr('tval');
					$( "#delete-confirm" ).dialog({
						resizable: false,
						height:160,
						width:260,
						modal: true,
						buttons: {
							"删除":	function() {
								[@c.url	var="backUrl" value='/manage/Config_site.action' scope="page"]
									[@c.param name="currentPage" value="${currentPage}"/]
								[/@c.url]
								[@c.url	var="deleteUrl" value='/manage/Config_delete.action' scope="page"]
									[@c.param name="backUrl" value="${backUrl}"/]
									[@c.param name="id" value=""/]
								[/@c.url]
								window.location	= "${deleteUrl}" + id;
								$( this	).dialog( "close" );
							},
							"取消":	function() {
								$( this	).dialog( "close" );
							}
						}
					});
					$( "#delete:ui-dialog" ).dialog( "destroy" );
					return false;
				})
				
				// 创建
				$('#createConfigItem').unbind('click').click(function() {
					[@c.url	var="createUrl" value='/manage/Config_create.action' scope="page"]
						[@c.param name="backurl" value="${currentUrl}"/]
					[/@c.url]
					var	url	= "${createUrl}";
					$.get(url, {'ajax':'true'}, function(data) {
						$('.config_edit').html(data);
						base.init();
					})
					
				})
			};
			
			// jqxComponentRender
			function _jqxComponentRender() {
				// Create jqxExpander
				$('.jqxExpander').jqxExpander({	showArrow: false, toggleMode: 'none', height: '500px', theme: base.config.theme	});
				
				// 增加子项事件绑定
				$('button', '#addConfigItem').click(function() {
					var size = $('.config_item').length;
					$('#addConfigItem').before("<tr class='config_item'><th>&nbsp;&nbsp;</th><td colspan=2> "
					+"<label>标题：</label><input style='width:80px;' name='config.configItemList["+size+"].name' value='' />"
					+" <label>键名：</label><input style='width:80px;' name='config.configItemList[" + size + "].key' value='' />"
					+" <label>键值：</label><input style='width:80px;' name='config.configItemList[" + size + "].value' value='' />"
					+" <input type='button' class='item-delete' value='删除' /></td></tr>");
					_itemDeleteEventListener()
					return false;
				})
			}
			
			// item-delete
			function _itemDeleteEventListener() {
				// 删除按钮
				$('.item-delete').unbind().click(function() {
					var id = $(this).attr('delId');
					if (id != null) {
						[@c.url	var="deleteItemUrl" value='/manage/Config_deleteItem.action' scope="page"]
									[@c.param name="backUrl" value="${currentUrl}"/]
								[/@c.url];
						$.post("${deleteItemUrl}", {id:id}, function(e){});
					}
					$(this).parent().parent('.config_item').fadeOut("slow", function(){
						$(this).remove();
					})
				})
			}
			return {
                config: {
                	url : null,
                    theme: null
                },
                init: function () {
                    _addEventListeners();
                    _jqxComponentRender();
                    _itemDeleteEventListener();
                }
			}
		}());
		
		$(function(){
			var link = "[@c.url value='/manage/Config_site.action?currentPage=${currentPage}' /]";
			base.config.theme = getTheme();
			base.config.url = link;
            //Initializing the form
			base.init();
		})
	</script>
[/#macro]

[#macro _content]
	<div class="jqxWidget">
		<div class="jqxExpander">
			<div>站点配置字典</div>	
			<div>
			[@demo.tableDisplayer id="id" renderTo="${renderTo}" ajaxPage="true"  property="siteConfigList" pagesize=4 page="/manage/Config_list.action" name="FreemarkerTable" config="/WEB-INF/component/table/simple.ftl" bundle="messageSource"]
				[@demo.column name="batch" title="批量" innerHtml="\linput type='checkbox' /\g" headerClass="batch" columnClass="span1"/]
				[@demo.column name="id" property="id" title="ID" headerClass="span1" columnClass="span1"/]
				[@demo.column name="path" property="title" title="标题" columnClass="span2"/]
				[@demo.column name="name" property="name" title="名称" columnClass="span3"/]
				[@demo.column name="type" property="type" title="类型" columnClass="span1"/]
				[@demo.column name="enabled" property="enabled" format="可用:禁用" title="启用状态" /]
				[@demo.column name="operate" property="id" title="操作" innerHtml="\la href='javascript:void(0);' act='edit' \g编辑\l/a\g&nbsp;|&nbsp;\la href='javascript:void(0);' act='delete' \g删除\l/a\g" headerClass="batch"/]
			[/@demo.tableDisplayer]
			</div>
		</div>
	</div>
	<div id="delete-confirm" style="display:none" title="删除字典">
		<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>配置字典删除后将无法恢复.</p>
		您确认删除吗？
	</div>
[/#macro]

[#if piece==true]
		[@_script /]
		<!--设置提交行为-->
		[#assign act="save" /]
		<!--设置table渲染到指定ID节点-->
		[#assign renderTo="#configList"]
		[@_content /]
[#else]
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>Insert title here</title>
		<meta name="menu" content="AdminMenu"/>
		<meta name="heading" content="heading"/>
		[@_script /]
	</head>
	<body id="body" class="config_content">
		<div class="configuration">
			<div id="configList" class="config_list">
				[@_content /]
			</div>
			<div class="config_edit">
		    	[@s.action name="Config_create" executeResult="true" namespace="/manage"]
		    		[@s.param name="ajax" value="true" /]
		    		[@s.param name="piece" value="true"/]
		    	[/@s.action]
			</div>
		</div>
	</body>
	</html>
[/#if]